package com.woniu.new_orange.finance_model_guo.controller;

import com.woniu.new_orange.entity.*;
import com.woniu.new_orange.finance_model_guo.Vo.FinallyOrderVo;
import com.woniu.new_orange.finance_model_guo.service.FinallyOrderService;
import com.woniu.new_orange.personnel_model_wu.service.AccountFundService;
import com.woniu.new_orange.personnel_model_wu.service.OrderFormService;
import com.woniu.new_orange.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Program: new_orange->FinallyOrderController
 * @Description:
 * @Author: 谭少琛的爸爸
 * @Date: 2023-02-02 16:52
 * @Version： 1.0
 **/
@RestController
@RequestMapping("finally")
@Api(tags = "最终订单表")
public class FinallyOrderController {
    @Resource
    private FinallyOrderService finallyOrderService;
    @Resource
    private OrderFormService orderFormService;
    @Resource
    private AccountFundService accountFundService;
    @ApiOperation(value = "查全部",notes = "进行查询全部")
    @GetMapping("queryAll")
    public Result query(){
        List<FinallyOrderVo> finalOrders = finallyOrderService.selectAll();
        return Result.ok().data("finallyList",finalOrders);
    }
    @GetMapping("{id}")
    public Result add1(@PathVariable long id){
//申请结算 --wu
         OrderForm orderForm = orderFormService.selectByPrimaryKey(id);
         FinalOrder finalOrder = new FinalOrder();
        String orderFormNo = orderForm.getOrderFormNo();
        finalOrder.setFinalOrderNo(orderFormNo);
       Integer orderFormId = orderForm.getOrderFormId();
        Date orderFormDate = orderForm.getOrderFormDate();
         BigDecimal orderFormBudget = orderForm.getOrderFormBudget();
        finalOrder.setOrderFormId(orderFormId);
        finalOrder.setFinalOrderCreateTime(orderFormDate);
        finalOrder.setFinalOrderActualPayment(orderFormBudget);
        finalOrder.setFinalOrderState("结束");
        int i = finallyOrderService.insertSelective(finalOrder);

        orderForm.setOrderFormState(2);
        System.out.println(orderForm);
         int i1 = orderFormService.updateByPrimaryKeySelective(orderForm);
        OrderForm orderForm2   = orderFormService.selectByPrimaryKey(id);
//余额 --wu
         AccounFunds accounFunds = accountFundService.selectByPrimaryKey(1l);
      BigDecimal accountFundsNum = accounFunds.getAccountFundsNum();
        BigDecimal num1 = accountFundsNum.subtract(orderFormBudget);
        accounFunds.setAccountFundsNum(num1);
        int i2 = accountFundService.updateByPrimaryKeySelective(accounFunds);


        return  Result.ok().data("orderList",orderForm2);
    }


//    zhang
    @PutMapping("submitFormfinalOrder")
    public Result submit(@RequestBody FinalOrder finalOrder){
        Date date = new Date();
        finalOrder.setFinalOrderCreateTime(date);

        int i = finallyOrderService.insertSelective(finalOrder);
        return Result.ok();
    }
}
